package com.du.dumall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.du.dumall.product.entity.AttrEntity;
import com.du.dumall.product.service.AttrAttrgroupRelationService;
import com.du.dumall.product.service.AttrService;
import com.du.dumall.product.vo.AttrGroupRelationVo;
import com.du.dumall.product.vo.WithattrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.du.dumall.product.entity.AttrGroupEntity;
import com.du.dumall.product.service.AttrGroupService;
import com.du.dumall.common.utils.PageUtils;
import com.du.dumall.common.utils.R;



/**
 * 属性分组
 *
 * @author dupeng
 * @email 13576245006@163.com
 * @date 2021-12-20 17:10:04
 */
@RestController
@RequestMapping("/product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @GetMapping("/{catalogId}/withattr")
    public R withattrList(@PathVariable("catalogId") Long catalogId){
         List<WithattrVo> vos =  attrService.withattrList(catalogId);
        return R.ok().put("data", vos);
    }

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page  =  attrService.noattrRelation(params,attrGroupId);
        return R.ok().put("page", page);
    }


    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrGroupService.deleteRelation(vos);
        return R.ok();
    }


    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveRelation(vos);
        return R.ok();
    }

    @GetMapping("/{attrGroupId}/attr/relation")
    public R listRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> attrEntityList =  attrService.listRelation(attrGroupId);
        return R.ok().put("data", attrEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
        PageUtils page = attrGroupService.queryPage(params,catId);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

		Long[] catelogPath = attrGroupService.getCatelogPath(attrGroup.getCatelogId());

		attrGroup.setCatelogPath(catelogPath);


        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
